const util = require('../../../../../../utils/util.js');
const api = require('../../../../../../config/api.js');
const app = getApp();
Page({
  data: {
    current: 0,
    //父级类目
    parentType: [],
    //子级类目
    sonType: [],
    //选中的数据
    selectId:[],
    //提示面板开关
    showTs: false
  },
  onLoad: function(){
    var _this = this
    var openid = app.globalData.logininfo.openid;
    var data = {};
    if (openid != null && openid != "") {
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        data.openid = openid;
        data.access_token = res.access_token;
        util.post(api.GetUserCatalog, data).then(function (res) {
          if (res.code == 200) {
            var transit = []
            for (var i = 0; i < res.result.json.interestTagList.length;i++){
              var catalogid = res.result.json.interestTagList[i].catalogid
              transit.push({ "type": 1, "catalogid": catalogid})
            }
            _this.setData({
              selectId: transit
            })
          }
        });
      });
    }
  },
  onReady: function(){
    var _this = this
    util.post(api.Home_LevelOneCategoryUrl).then(function (res) {
      if (res.code == 200) {
        res.result.list.splice(res.result.list.length - 1, 1)
        for (var i = 0; i < res.result.list.length; i++) {
          res.result.list[i].pitch = false
        }
        _this.setData({
          parentType: res.result.list
        })
        var arr = _this.data.parentType
        var noindex = _this.data.current
        var pid = arr[noindex].id
        var data = {};
        data.id = pid;
        util.post(api.Home_GetCatalogLabelByParentidUrl, data).then(function (res2) {
          if (res2.code == 200) {
            _this.setData({
                sonType: res2.result.personalityLabelList
            })
            _this.checkType();
          }
        })
      }
    });
    setTimeout(function(){
      _this.move1();
    },1000)
    setTimeout(function () {
      _this.move2();
    }, 2000)
    setTimeout(function () {
      _this.move1();
    }, 3000)
    setTimeout(function () {
      _this.move2();
    }, 4000)
    setTimeout(function () {
      _this.move1();
    }, 5000)
    setTimeout(function () {
      _this.move2();
      _this.closeTs();
    }, 6000)
  },
  //滑动父级类目
  changelm: function(e){
    var _this = this
    var arr = _this.data.parentType
    var sel = e.detail.current
    _this.setData({
      current: sel
    })
    var pid = arr[sel].id
    var data = {};
    data.id = pid;
    util.post(api.Home_GetCatalogLabelByParentidUrl, data).then(function (res2) {
      if (res2.code == 200) {
        _this.setData({
          sonType: res2.result.personalityLabelList
        })
        _this.checkType();
      }
    })
  },
  //选择类目
  selectType: function(e){
    var _this = this
    var sId = e.currentTarget.dataset.id
    var arr = _this.data.selectId
    var openadd = true
    if (arr.length!=0){
      for(var i = 0;i<arr.length;i++){
        if (sId == arr[i].catalogid){
          openadd = false
          arr.splice([i], 1)
          _this.setData({
            selectId: arr
          })
        }
      }
      if (openadd){
        var sel = { "type": 1, "catalogid": sId }
        arr.push(sel)
        _this.setData({
          selectId: arr
        })
      }   
    }else{
      var sel = { "type": 1, "catalogid": sId }
      arr.push(sel)
      _this.setData({
        selectId: arr
      })
    }
    console.log(_this.data.selectId)
    _this.checkType();
  },
  //检查选中类目
  checkType: function(){
    var _this = this
    var count = 0
    var arr = _this.data.selectId
    var list = _this.data.sonType
    for (var i = 0; i < list.length; i++) {
      list[i].pitch = false
    }
    var parentList = _this.data.parentType
    var sel = _this.data.current
    if (arr.length!=0){
      for (var i = 0; i < arr.length;i++){
        for (var j = 0; j < list.length; j++) {
          if (arr[i].catalogid == list[j].id){
            count++;
            list[j].pitch = true
          }
        }
      }
    }else{
      for (var i = 0; i < list.length; i++) {
        list[i].pitch = false
      }
    }
    _this.setData({
      sonType: list
    })
   if (count!=0){
      parentList[sel].pitch = true
      _this.setData({
        parentType: parentList
      })
    }else{
      parentList[sel].pitch = false
      _this.setData({
        parentType: parentList
      })
    }
    _this.setData({
      pitchNumber: arr.length
    })
  },
  //保存选中的标签
  saveLable: function(){
    var _this = this
    var openid = app.globalData.logininfo.openid;
    var data = {};
    if (openid != null && openid != "") {
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        data.openid = openid;
        data.access_token = res.access_token;
        data.param = JSON.stringify(_this.data.selectId)
        util.post(api.SaveUserCatalog, data).then(function (res) {
          if (res.code == 200) {
            wx.navigateBack({
              delta: 2
            })
          }
        });
      });
    }
  },
  //手指移动
  move1: function () {
    var _this = this
    var ani = wx.createAnimation({
      duration: 1000,
      timingFunction: 'linear',
    })
    ani.left(-15).step()
    _this.setData({
      szani: ani
    })
  },
  //手指移动
  move2: function () {
    var _this = this
    var ani = wx.createAnimation({
      duration: 1000,
      timingFunction: 'linear',
    })
    ani.left(20).step()
    _this.setData({
      szani: ani
    })
  },
  //关闭提示
  closeTs: function(){
    this.setData({
      showTs: true
    })
  }
})
